$(document).ready(function(){
	getAll();

	$.getJSON('controller/cont.cuti.php',{get_ref_cuti:true},function(data){
        $.each(data,function(index,data){
            var option2 = $('<option />');
            option2.attr('value', data.kode).text(data.uraian); 
            $('#id_cuti_peg').append(option2);
        });
    });

    $("#btn_simpan_ubah_peg").click(function(){
        var id=$("#id_usulan_peg").val();
        var tglAwal=$("#tgl_mulai").val();
        var tglSelesai=$("#tgl_selesai").val();
        var nip=$("#nip_peg").val();
        var nipAtasan=$("#nip_atasan").val();

        var e = document.getElementById('id_cuti_peg');
        var id_cuti = e.options[e.selectedIndex].value;
        var f = document.getElementById('id_tujuan_peg');
        var id_tujuan = f.options[f.selectedIndex].value;

        if (nip === '' || tglAwal === '' || tglSelesai === '') {
            $('#error2').html('Tidak Boleh Kosong').fadeIn(500).delay(2500).fadeOut(500);
        }else if(tglAwal> tglSelesai){
            $('#error2').html('Tgl awal cuti harus lebih awal dr tgl akhir cuti').fadeIn(500).delay(2500).fadeOut(500);
        } else {
            $.post('controller/cont.usulan_peg.php',{aksi_simpan_ubah_usulan:true,id:id,nip:nip,nipAtasan:nipAtasan,tglAwal:tglAwal,tglAkhir:tglSelesai,id_cuti:id_cuti,id_tujuan:id_tujuan},function(data){
                if(data.msg){
                    $('#success2').html('Data Berhasil Diubah').fadeIn(500).delay(2500).fadeOut(500);
                    clear_input_peg();
                }else{
                    $('#error2').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                    clear_input_peg();
                }
                getAll();
            },'json');
            
        }
        return false;
    });

    $("#btn_simpan_peg").click(function(){
    	var tglAwal=$("#tgl_mulai").val();
    	var tglSelesai=$("#tgl_selesai").val();
    	var nip=$("#nip_peg").val();
        var nipAtasan=$("#nip_atasan").val();

    	var e = document.getElementById('id_cuti_peg');
        var id_cuti = e.options[e.selectedIndex].value;
        var f = document.getElementById('id_tujuan_peg');
        var id_tujuan = f.options[f.selectedIndex].value;

        if (nip === '' || tglAwal === '' || tglSelesai === '') {
            $('#error2').html('Tidak Boleh Kosong').fadeIn(500).delay(2500).fadeOut(500);
        }else if(tglAwal> tglSelesai){
            $('#error2').html('Tgl awal cuti harus lebih awal dr tgl akhir cuti').fadeIn(500).delay(2500).fadeOut(500);
        } else {
	        $.post('controller/cont.usulan_peg.php',{aksi_simpan_usulan:true,nip:nip,nipAtasan:nipAtasan,tglAwal:tglAwal,tglAkhir:tglSelesai,id_cuti:id_cuti,id_tujuan:id_tujuan},function(data){
	        	if(data.msg){
	        		$('#success2').html('Data Berhasil Direkam').fadeIn(500).delay(2500).fadeOut(500);
	        		clear_input_peg();
	        	}else{
	        		$('#error2').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
	        		clear_input_peg();
	        	}
                getAll();
	        },'json');            
    	}
    	return false;
    });
});

function clear_input_peg(){
	document.getElementById("tgl_mulai").value="";
	document.getElementById("tgl_selesai").value="";
    document.getElementById("nip_atasan").value="";
}

function buat(){
	$.getJSON('controller/cont.usulan_peg.php',{akse_get_data:true},function(data){
		document.getElementById("nip_peg").value=data;
	});
}

function hapus_usulan(id){
    var retval;
      retval = confirm("Anda yakin untuk meng HAPUS record ini ? \nPERHATIAN : Data yang telah dihapus tidak dapat dikembalikan lagi!");
      if (retval) {
        $.getJSON('controller/cont.usulan_peg.php',{aksi_hapus_usulan:true,id:id},function(data){
            if(data.msg){
                $('#success').html('Data Berhasil Dihapus').fadeIn(500).delay(2500).fadeOut(500);
                clear_input_peg();
            }else{
                $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                clear_input_peg();
            }
            getAll();
        });
      }else{
        alert("Penghapusan Batal");
      }
}

function ubah_usulan(id){
	$.getJSON('controller/cont.usulan_peg.php',{akse_get_data_ubah:true,id:id},function(data){
		if(data.msg){
            document.getElementById("id_usulan_peg").value=data.usulan.id;
			document.getElementById("nip_peg").value=data.usulan.nip;
            document.getElementById("nip_atasan").value=data.usulan.nipAtasan;
			document.getElementById("tgl_mulai").value=data.usulan.tglAwal;
            document.getElementById("tgl_selesai").value=data.usulan.tglAkhir;

            var ddl = document.getElementById('id_cuti_peg');
            var opts = ddl.options.length;
            for (var i = 0; i < opts; i++) {
                if (ddl.options[i].value == data.id_cuti) {
                    ddl.options[i].selected = true;
                    break;
                }
            }

            var ddl2 = document.getElementById('id_tujuan_peg');
            var opts2 = ddl2.options.length;
            for (var i = 0; i < opts2; i++) {
                if (ddl2.options[i].value == data.id_tujuan) {
                    ddl2.options[i].selected = true;
                    break;
                }
            }

			document.getElementById("btn_simpan_peg").style.display="none";
            document.getElementById("btn_simpan_ubah_peg").style.display="block";
		}else{
			alert("error");
		}
	});
}
function cetak_usulan(id){
    document.getElementById("id_usulan_cetak").value=id;
    document.getElementById("aksi_usulan_cetak").value="cetak";
    $("#frm_cetak").submit();
}

function getAll(){
    $.getJSON('controller/cont.usulan_peg.php',{aksi_get_usulan:true},function(data){
        $('#list_usulan tbody').html('');
        var tab="";
        if(data.msg){
            $.each(data.usulan, function(index, usulan) {
                tab += "<tr>";
                tab += "<td>" + usulan.usulan.date_created +"</td>";               
                tab += "<td>" + usulan.ref + "</td>";
                tab += "<td>" + usulan.tujuan + "</td>";
                tab += "<td>" + usulan.usulan.tglAwal+" s.d "+usulan.usulan.tglAkhir+ "</td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModal\" onclick=\"ubah_usulan(" + usulan.usulan.id + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-edit\"></span>&nbsp;Ubah</a></td>";
                tab += "<td align=\"center\"><button type=\"button\" onclick=\"hapus_usulan(" + usulan.usulan.id + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-remove-circle\"></span>&nbsp;Hapus</button></td>";
                tab += "<td align=\"center\"><button type=\"button\" onclick=\"cetak_usulan(" + usulan.usulan.id + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-print-circle\"></span>&nbsp;Cetak</button></td>";
                tab += "</tr>";
                //alert(tab);
                
            });
            $('#list_usulan tbody').html(tab);
                $('#list_usulan').dataTable({
                    "sPaginationType": "bs_full",
                    "bInfo": false,
                    "bLengthChange": false,
                    "iDisplayLength": 5,
                  }); 
                  $('#list_usulan').each(function(){
                    var datatable = $(this);
                    // SEARCH - Add the placeholder for Search and Turn this into in-line form control
                    var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
                    search_input.attr('placeholder', 'Search');
                    search_input.addClass('form-control input-sm');
                    // LENGTH - Inline-Form control
                    var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
                    length_sel.addClass('form-control input-sm');
                  });

        }else{
            $('#list_usulan tbody').append('<td colspan="9">Data Kosong</td>');
        }
    });
}